<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  

  <script>
    var data = [
      {
        name: "所有物品",
        children: [
            {
              name: "水果",
              children: [
                {
                  name: "苹果",
                  children: [
                    {
                      name: '青苹果'
                    },
                    {
                      name: '红苹果'
                    }
                  ]
                }]
            },
            {
              name: '主食',
              children: [
                {
                  name: "米饭",
                  children: [
                    {
                      name: '北方米饭'
                    },
                    {
                      name: '南方米饭'
                    }
                  ]
                }
              ]
            },
            {
              name: '生活用品',
              children: [
                  {
                    name: "电脑类",
                    children: [
                      {
                        name: '联想电脑'
                      },
                      {
                        name: '苹果电脑'
                      }
                    ]
                  },
                  {
                    name: "工具类",
                    children: [
                      {
                        name: "锄头"
                      },
                      {
                        name: "锤子"
                      }
                    ]
                  },
                  {
                    name: "生活用品",
                    children: [
                      {
                        name: "洗发水"
                      },
                      {
                        name: "沐浴露"
                      }
                    ]
                  }
              ]
            }
        ]
      }
    ]
    
    
    //递归遍历实现
    // function getStr (list) {
    //   list.forEach((row) => {
    //     if (row.children) {
    //       // row.children['title'] = 1
    //       getStr(row.children)
    //       console.log(row)
    //     }
    //   })
    // }
    // getStr(data)
      
    function fn (list) {
      list.forEach(item => {
        console.log(item)
        if (item.children) {
          fn(item.children)
        }
      })
    }
    fn(data)
  </script>
</body>
</html>